package com.demarc.pesquisas.web.converter;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;

import org.apache.commons.lang3.StringUtils;

import com.demarc.pesquisas.infraestrutura.util.enumeracao.IEnumeracaoComCharDescricao;

public abstract class AbstractEnumeracaoComCharDescricaoConverter implements
		Converter {

	/**
	 * {@inheritDoc}
	 */
	@Override
	public Object getAsObject(FacesContext context, UIComponent component,
			String input) {

		IEnumeracaoComCharDescricao object = null;
		if (StringUtils.isNotBlank(input)) {
			object = getEntity(input);
		}
		return object;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public String getAsString(FacesContext context, UIComponent component,
			Object input) {

		String value = null;
		if (input instanceof IEnumeracaoComCharDescricao) {
			value = String.valueOf(((IEnumeracaoComCharDescricao) input)
					.getCodigo());
		}
		return value;
	}

	protected abstract IEnumeracaoComCharDescricao getEntity(String id);
}
